package org.jeecg.modules.geo.service.resolve.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.bim.entity.BimAreas;
import org.jeecg.modules.bim.service.IBimAreasService;
import org.jeecg.modules.geo.core.constant.BimGeoEventType;
import org.jeecg.modules.geo.entity.BimGeoEvent;
import org.jeecg.modules.geo.model.dto.AreasDTO;
import org.jeecg.modules.geo.model.vo.AreasVO;
import org.jeecg.modules.geo.service.resolve.GeoBaseResolve;
import org.jeecg.modules.geo.service.resolve.GeoBaseService;
import org.jeecg.modules.geo.service.resolve.IGeoResolves;
import org.jeecg.modules.icon.entity.BimGeoIcon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AreasGeoResolveServiceImpl implements IGeoResolves {

    @Autowired
    private IBimAreasService bimAreasService;

    public List<AreasDTO> resolveAreas(List<AreasDTO> areas, List<BimGeoEvent> geoEventConfigResults, List<BimGeoIcon> geoIconConfigResults) {
        if (areas == null || (geoEventConfigResults == null && geoIconConfigResults == null)) {
            return areas;
        }

        Map<Integer, Map<Integer, List<BimGeoEvent>>> defaultEventsAll = GeoBaseService.getGeoDefaultEvent(geoEventConfigResults, BimGeoEventType.AREA_EVENT_TYPE.getValue());
        Map<String, List<BimGeoEvent>> eventsGroupByName = GeoBaseService.getGeoEventGroupbyName(geoEventConfigResults, BimGeoEventType.AREA_EVENT_TYPE.getValue());

        Map<Integer, List<BimGeoIcon>> defaultIcons = GeoBaseService.getGeoDefaultIcons(geoIconConfigResults, BimGeoEventType.AREA_EVENT_TYPE.getValue());
        Map<String, List<BimGeoIcon>> iconGroupByName = GeoBaseService.getGeoIconGroupByName(geoIconConfigResults, BimGeoEventType.AREA_EVENT_TYPE.getValue());

        //解析鼠标事件
        for (AreasDTO area : areas) {
            String leftAction = area.getLeftAction();
            HashMap<String, Object> clicks = new HashMap<>();
            if ((leftAction != null && !leftAction.isEmpty() && eventsGroupByName != null) || defaultEventsAll != null) {
                //默认只有一个事件
                Map<String, Object> clickLeft = GeoBaseResolve.resolveLeftEvent(defaultEventsAll, eventsGroupByName, leftAction, area.getParams(), BimGeoEventType.AREA_EVENT_TYPE.getValue());
                clicks.put("clickLeft", clickLeft);
            }
            String rightAction = area.getRightAction();
            if ((rightAction != null && !rightAction.isEmpty() && eventsGroupByName != null) || defaultEventsAll != null) {
                Map<String, Object> clickRight = GeoBaseResolve.resolveRightEvent(defaultEventsAll, eventsGroupByName, rightAction, area.getParams(), BimGeoEventType.AREA_EVENT_TYPE.getValue());
                clicks.put("clickRight", clickRight);
            }
            area.setAction(clicks);

            String customAreaCss = area.getCustomAreaCss();
            if (defaultIcons != null || (StringUtils.isNotEmpty(customAreaCss) && iconGroupByName != null)) {
                HashMap<String, Object> icons = GeoBaseResolve.resolveIcons(defaultIcons, iconGroupByName, customAreaCss, BimGeoEventType.AREA_EVENT_TYPE.getValue());
                area.setAreaCss(icons);
            }
        }
        return areas;
    }


    @Override
    public List<AreasDTO> resolveAreas(List<AreasDTO> areas, Long projectId) {
        List<BimAreas> bimAreas = bimAreasService.queryAreas(null, projectId);
        if (CollUtil.isNotEmpty(bimAreas)) {
            log.info("获取bim区域数据,第三方项目id未在系统=>{}中查询到", projectId);
            return areas;
        }
        Map<String, String> areaMap = bimAreas.stream().collect(Collectors.toMap(BimAreas::getName, BimAreas::getAreas));
        //解析鼠标事件
        for (AreasDTO area : areas) {
            String areaCss = area.getCustomAreaCss();
            if (StringUtils.isNotBlank(areaCss)) {
                String areaInfo = areaMap.get(areaCss);
                if (StringUtils.isNotBlank(areaInfo)) {
                    AreasVO areasVO = JSON.parseObject(areaInfo, AreasVO.class);
                    if (Objects.nonNull(areasVO)) {
                        areasVO.setAction(areasVO.getAction());
                        area.setAreaCss((HashMap<String, Object>) areasVO.getAreaCss());
                    }
                }
            }
        }
        return areas;
    }

}
